using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fated.SaveEngine;

namespace Fated.Character {
    internal class AllyCollection : IJSON {
        List<AllyData> allies;

        public AllyCollection() {
            allies = new List<AllyData>();
        }
        public AllyCollection(Dictionary<string, object> input) {
            allies = new List<AllyData>();
            setData(input);
        }

        public void setData(Dictionary<string, object> input) {
            allies.Clear();
            foreach (object o in input) {
                allies.Add(new AllyData((Dictionary<string, object>)o));
            }
        }

        public void addAlly(AllyData newAlly) {
            allies.Add(newAlly);
        }
        public bool containsName(String allyName) {
            foreach (AllyData d in allies) {
                if (d.Name == allyName) {
                    return true;
                }
            }
            return false;
        }
        public void removeAlly(String allyName) {
            for (int i = 0; i < allies.Count(); i++) {
                if (allies[i].Name == allyName) {
                    allies.RemoveAt(i);
                    break;
                }
            }
        }

        public int Count() {
            return allies.Count();
        }
        public AllyData getAlly(int index) {
            return allies[index];
        }
        public AllyData getAlly(string allyName) {
            for (int i = 0; i < allies.Count(); ++i) {
                if (allies[i].Name == allyName) {
                    return allies[i];
                }
            }
            return null;
        }

        public Dictionary<string, object> ToJSON() {
            Dictionary<string, object> output = new Dictionary<string, object>();
            for (int i = 0; i < allies.Count(); i++) {
                output.Add(i.ToString(), allies[i].ToJSON());
            }
            return output;
        }

        internal void mergeData(List<Dictionary<string, object>> inputData) {
            foreach (Dictionary<string, object> d in inputData) {
                bool dataFound = false;
                AllyData temp = new AllyData(d);
                for (int i = 0; i < allies.Count(); i++) {
                    if (temp.Name == allies[i].Name) {
                        allies[i] = temp;
                        dataFound = true;
                        break;
                    }
                }
                if (!dataFound) {
                    // this could be replaced by code that adds in the ally data, but
                    // if this is ever triggered, it means that data could have been lost elsewhere
                    // So hopefully this will never happen.
                    throw new Exception("Post-battle ally data lost? False ally data gained?");
                }
            }
        }
    }
}
